package com.niuwa.p2p.action.accountmanager;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.niuwa.p2p.action.JsonAction;
import com.niuwa.p2p.core.mvc.entity.JsonResult;
import com.niuwa.p2p.core.mvc.entity.PageBean;
import com.niuwa.p2p.entity.fund.AccountFlow;
import com.niuwa.p2p.entity.fund.NwAccount;
import com.niuwa.p2p.service.NWAccountManager.INWAccountManagerService;
import com.opensymphony.xwork2.ActionContext;

/**
 * 
 * @ClassName: UserAction
 * @Descriptio 用户Action
 * @author:    jishengjun
 * 
 */

public class NWAccountManageAction extends JsonAction{

	private static final long serialVersionUID = -8900124777631535966L;

	/** 日志 */
	private static Logger logger = Logger.getLogger(NWAccountManageAction.class);
	
	//账户类型
	private String accountType;
	
	@Autowired
	private  INWAccountManagerService   nwAccountManagerService;
	
	//牛娃账户类
    NwAccount NwAccount=new NwAccount();   



	/*	*
	 * 
	 *跳转到牛娃账户管理页面
	 *
	 */
	@Action(value="NWAccountManage")
	public String gotoNWAccountManagePage(){
		return "NWAccountManage";
		
	}
	
	/*
	 * 
	 *跳转到增加NW账户
	 * @return String
	 *
	 */
	@Action(value="gotoAddNWAccountPage", 
			results={@Result(location="addNWAccount.jsp", name=SUCCESS)})
	public String gotoAddNWAccountPage(){
		return SUCCESS;
	}


	
	//查询开户机构
	@Action(value="searchOpenAccount")
	public String serachOpenAccount(){
		List<Map<String,Object>> openAccount;
		try {
			openAccount = nwAccountManagerService.serachOpenAccount(accountType);
			setObjectReSult(openAccount);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return NONE;
		
	}

	

	
	/**
	 *查询牛娃账户列表
	 * @return
	 * @throws Exception
	 */
	@Action(value = "nwAccountList")
	public String userList() throws Exception {
		setObjectReSult(queryPage());
		logger.info("nwAccountList action execute success");
		return NONE;
	}
	
	
	/**
	 *查询资金记录列表
	 * @return
	 * @throws Exception
	 */
	@Action(value = "accountFlowList")
	public String accountFlowList() throws Exception {
		setObjectReSult(queryPage2());
		logger.info("accountFlowList action execute success");
		return NONE;
	}
	
	
	/**
	 * 分页查询
	 * 
	 * @return Pagination<NwAccount> 
	 */
	private PageBean<NwAccount> queryPage() {

		// 获取查询参数
		Map<String, Object> paramMap = getParamMap();

		// 查选分页集合
		PageBean<NwAccount> page =  nwAccountManagerService.search(paramMap);
		
		return page;
	}
	
	
	
	/**
	 * 分页查询
	 * 
	 * @return Pagination<AccountFlow> 
	 */
	private PageBean<AccountFlow> queryPage2() {

		// 获取查询参数
		Map<String, Object> paramMap = getParamMap();

		// 查选分页集合
		PageBean<AccountFlow> page =  nwAccountManagerService.searchAccountflowByAcctNo(paramMap);
		
		return page;
	}
	
	
  //根据账户号查询明细
	@Action(value="getNwAccountByAcctNo")
	  public String getNwAccountByAcctNo(){
		NwAccount =	nwAccountManagerService.getNwAccountByAcctNo(NwAccount.getAcctNo());
		setObjectReSult(NwAccount);
		return NONE;               
	}
	
	/**
	 * 
	 *增加牛娃账户
	 *
	 */
	@Action(value="addNWAccount")
	public String addNWAccount(){
		String message=new String();
        JsonResult results=new JsonResult();	
	try {
		List<NwAccount> nwAccountList=nwAccountManagerService.getNwAccountByNo(NwAccount);
		 if(nwAccountList.size()!=0){
			 message="账户名重复,新增失败";
			 throw new RuntimeException();
		 }else{
	          nwAccountManagerService.addNWAccount(NwAccount);	
			 message="新增成功";
			 results.setMsg(message);
			 setResult(results);
		 }
	} catch (Exception e) {
		if(!("账户名重复,新增失败".equalsIgnoreCase(message))){
		   message="新增失败";
		}
		 results.setMsg(message);
		  setResult(results);
		  e.printStackTrace();	
	}
	return NONE;            

}


	
	/**
	 * 
	 *修改牛娃账户
	 *
	 */
	@Action(value="updateNwAccountByAcctNo")
	public String updateNwAccountByAcctNo(){
		String message=new String();
        JsonResult results=new JsonResult();	
	try {
		List<NwAccount> nwAccountList=nwAccountManagerService.getNwAccountByNo(NwAccount);
		 if(nwAccountList.size()==0){
			 message="账户不存在,修改失败";
			 throw new RuntimeException();
		 }else{
	          nwAccountManagerService.updateNwAccountByAcctNo(NwAccount);	
			 message="修改成功";
			 results.setMsg(message);
			 setResult(results);
		 }
	} catch (Exception e) {
		if(!("账户不存在,修改失败".equalsIgnoreCase(message))){
		   message="修改失败";
		}
		 results.setMsg(message);
		  setResult(results);
		  e.printStackTrace();	
	}
	return NONE;            

}
	public String getAccountType() {
		return accountType;
	}

	public void setAccountType(String accountType) {
		this.accountType = accountType;
	}

	public INWAccountManagerService getNwAccountManagerService() {
		return nwAccountManagerService;
	}

	public void setNwAccountManagerService(
			INWAccountManagerService nwAccountManagerService) {
		this.nwAccountManagerService = nwAccountManagerService;
	}

	public NwAccount getNwAccount() {
		return NwAccount;
	}

	public void setNwAccount(NwAccount nwAccount) {
		NwAccount = nwAccount;
	}

	
}
